/**
 * 
 */

package com.the9tcat.everydayapp.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.the9tcat.everydayapp.data.AppInfo;
import com.the9tcat.hadi.DefaultDAO;

/**
 * @author jianhpin
 */
public class AppDatabase {

    public static boolean saveAppInfo(Context con, AppInfo info) {
    	
    	boolean result = true;
    	
        DefaultDAO dao = new DefaultDAO(con);
        @SuppressWarnings("unchecked")
        List<AppInfoTable> app = (List<AppInfoTable>) dao.select(AppInfoTable.class, false, " id = ?", new String[] {info.id}, null, null, null, null);
        if (app != null && app.size() != 0) {
        	return true;
        }
        /** for save data to database */
        AppInfoTable ait = new AppInfoTable();
        ait.id = info.id;
        ait.description = info.description;
        ait.pack_name = info.pack_name;
        ait.name = info.name;
        ait.recommend = info.recommend;
        ait.sex = info.sex;
        ait.target_user = info.target_user;
        
        List<DownlinkTable> list = new ArrayList<DownlinkTable>();
        for (int i = 0; i < info.down_link.length; i++) {
            DownlinkTable dt = new DownlinkTable();
            dt.id = info.id;
            dt.link = info.down_link[i].link;
            dt.market = info.down_link[i].market;
            dt.down_time = info.down_link[i].down_time;
            list.add(dt);
        }
        
        try {
        	dao.insert(ait);
        	dao.insert_list(list);
        } catch(Exception e) {
        	result =  false;
        }
        
        File iconDir = new File(con.getFilesDir().getAbsolutePath() + "/icon");
        File screenshotDir = new File(con.getFilesDir().getAbsolutePath() + "/screenshot");
        try {
            if (!iconDir.exists()) {
                iconDir.mkdir();
            }
            if (!screenshotDir.exists()) {
                screenshotDir.mkdir();
            }
            File icon = new File(iconDir.getAbsolutePath() + "/icon_" + info.id + ".png");
            if (!icon.exists()) {
            	FileOutputStream fop = new FileOutputStream(icon);
            	InputStream is = new URL(info.icon_url).openConnection().getInputStream();
            	byte[] buffer = new byte[512];
            	int len = 0;
            	while ((len = is.read(buffer)) != -1) {
            		fop.write(buffer, 0, len);
            	}
                fop.flush();
                fop.close();
            }
            File screenshot = new File(screenshotDir.getAbsolutePath() + "/screenshot_" + info.id + ".png");
            if (!screenshot.exists()) {
            	FileOutputStream fop2 = new FileOutputStream(screenshot);
            	InputStream is = new URL(info.screenshot_url).openConnection().getInputStream();
            	byte[] buffer = new byte[512];
            	int len = 0;
            	while ((len = is.read(buffer)) != -1) {
            		fop2.write(buffer, 0, len);
            	}
                fop2.flush();
                fop2.close();
            }
        } catch (Exception e) {
        	result = false;
        }
        return result;
    }
}
